package com.hehan.hhdemo.controller;

import com.hehan.hhdemo.api.ResponseVo;
import com.hehan.hhdemo.common.exception.BusinessException;
import com.hehan.hhdemo.core.annotation.SystemLog;
import com.hehan.hhdemo.service.IUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by hehan on 2018/11/16.
 */
@RestController
@RequestMapping("upload")
@Api("文件上传接口")
public class UploadController {

    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);

    @Autowired
    private IUploadService uploadService;

    @ApiOperation("通用文件上传接口")
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    @SystemLog("上传接口")
    public ResponseVo uploadSku(@RequestParam("file") MultipartFile file) {
        try {
            String imgUrl = uploadService.uploadFile(file);
            return ResponseVo.success();
        } catch (Exception e) {
            logger.info("upload file fail,fileName {} ", file.getOriginalFilename(), e);
            return ResponseVo.failure(new BusinessException(e.getMessage()));
        }
    }

}
